package com.micmac.main.view.compenents
{
	
	/*
	
	Author : Roy Raul.
	
	Component : BasePageMediator             
	
	Version History         : 0.1 Date Jul-20-2011
	
	Current Version         : 0.1 Date Jul-20-2011
	
	Build Date              : Apr-14-2010
	
	*/
	
	
	import com.AppConst;
	import com.micmac.customer.view.NotificationView;
	import com.micmac.main.events.ModuleLoadEvent;
	import com.micmac.main.events.NotificationCloseEvent;
	import com.micmac.main.events.NotificationOnEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getDefinitionByName;
	
	import mx.controls.Alert;
	import mx.core.IVisualElement;
	import mx.core.UIComponent;
	import mx.events.FlexEvent;
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	
	import org.robotlegs.mvcs.Mediator;
	import org.robotlegs.utilities.modular.mvcs.ModuleMediator;
	
	import spark.modules.Module;
	import spark.modules.ModuleLoader;
	
	
	
	public class BasePageMediator extends ModuleMediator
	{
		[Inject]
		public var basePage:BasePage;
		private var moduleView:*;
		
		private var currentMediator:Class;
		private var currentView:Class;
		
		[Inject]
		private var _notificationView:NotificationView;
		private var _notificationTimer:Timer;
		
		override public function onRegister():void
		{
			//dispatch(new RegisterEvent(RegisterEvent.REGISTER_CUSTOMER_CONTROLL));
			
			addViewListener(BasePage.SEND,onClick);
			addModuleListener(NotificationOnEvent.NOTIFICATION_ON,onAddNotification,NotificationOnEvent);
			eventMap.mapListener(eventDispatcher,ModuleLoadEvent.LOAD_MODULE,onLoadModule);
		}
		
		private function onClick(e:Event):void
		{
			dispatchToModules(new Event(AppConst.TEST_REGISTER));
		}
		
		private function onLoadModule(event:ModuleLoadEvent):void
		{
			var modulePath:String = event.modulePath;
			
			if(basePage.moduleLoader.numElements!=0)
			{
				basePage.moduleLoader.unloadModule();
			}
			
			basePage.moduleLoader.loadModule(modulePath);
			//loadModule(modulePath);
		}
		
		
		
		private function onAddNotification(event:NotificationOnEvent):void
		{
			_notificationView = new NotificationView;
			_notificationView.content = event.content;
			_notificationView.currentState = 'signedon';
			_notificationView.addEventListener(NotificationCloseEvent.NOTIFICATION_CLOSED,onCloseNotification);
			basePage.notificationContainer.addElementAt(_notificationView,basePage.notificationContainer.lastIndexInView +1);
			_notificationTimer = new Timer(0,500);
			_notificationTimer.addEventListener(TimerEvent.TIMER_COMPLETE,onNotificationComplete);
			_notificationTimer.start();
		}
		
		private function onNotificationComplete(e:TimerEvent):void
		{
			basePage.notificationContainer.removeAllElements();
			_notificationTimer.stop();
		}
		
		private function onCloseNotification(event:NotificationCloseEvent):void
		{
			basePage.notificationContainer.removeElementAt(basePage.notificationContainer.getElementIndex(event.notificationView));
		}
		
	}
}